<?php
class Core_Data_Mappers_User extends Core_Data_Mappers_Abstract{
	protected $_businessclass = "Core_Business_User";
	protected $_modelclass = "Core_Data_Models_User";
	protected $_map=array("Id"=>"id","Email"=>"email","Salutation"=>"salutation","Firstname"=>"firstname",
	"Lastname"=>"lastname","Zipcode"=>"zipcode","City"=>"city","Created"=>"created","Updated"=>"updated",
	"Validated"=>"validated","Disabled"=>"disabled","Password"=>"password","Payment"=>"paymentrange","Linkviadeo"=>"linkviadeo","Linklinkedin"=>"linklinkedin","Token"=>"token");
	
	
	public function find($id){
		if(isset($id)) {
		$rows = $this->getDbTable()->fetchAll(array("id=?"=>$id));
		if(isset($rows)) {
			$output = $this->map($rows->current());
			return $output;
		}	
		}
		return null;
	}
	public function add($object) {
		if($object instanceof $this->_businessclass /*&& !$this->exists($object)*/) {
			$object->genToken();
			$data = $this->reversemap($object);
			unset($data['id']);
			//$data['password']=sha1($object->Password);
			
			$id = $this->getDbTable()->insert($data);
			$object->Id = $id;
		}
	}
	public function edit($object) {
		if($object instanceof $this->_businessclass && $this->exists($object)) {
			$data = $this->reversemap($object);
			unset($data['id']);
			if($data['password']=="")
				unset($data['password']);
			//$data['password']=sha1($object->Password);
			$this->getDbTable()->update($data,array("id=?"=>$object->Id));
		}
	}
	public function exists($object) {
		if($object instanceof $this->_businessclass && null!==$this->find($object->getId()))
			return true;
		else 
			return false;
	}
	public function fetchAll($where=null,$count=10,$start=0) {
		$rows = $this->getDbTable()->fetchAll($where,null,$count,$start);
		if(isset($rows) && count($rows)>0) {
			$collection = array();
			foreach ($rows as $item) {
				$collection[] = $this->map($item);
			}
			return $collection;
		}
		return null;
	}
}